package pl.edu.mimuw.mas.agent;

import jade.core.AID;
import pl.edu.mimuw.mas.agent.behaviour.DFRegisterBehaviour;
import pl.edu.mimuw.mas.agent.behaviour.hunting.hunter.ScaringBehaviour;
import pl.edu.mimuw.mas.agent.behaviour.hunting.hunter.ShootBehaviour;
import pl.edu.mimuw.mas.agent.behaviour.hunting.hunter.ShootSynchronizationServerBehaviour;
import pl.edu.mimuw.mas.chart.Chart;
import pl.edu.mimuw.mas.util.AgentColor;
import pl.edu.mimuw.mas.util.PositionHelper;

public class HunterAgent extends HuntingAgent {

	private static final long serialVersionUID = -7225359392465935302L;
	
	public final static String HUNTER_SERVICE_TYPE = "hunter-service";
	
	public static final String SHOOT_INFO_CONV = "shoot-info-conv";
	public static final String BEST_PAIR_CONV = "best-pair-conv";
	
	private long lastShootTime = 0; 
	private AID lastShooter = null;
	
	private double scareFactor;
	private AID pairedLeader = null;

	public AID getLastShooter() {
		return lastShooter;
	}

	public void setLastShooter(AID lastShooter) {
		this.lastShooter = lastShooter;
	}

	public long getLastShootTime() {
		return lastShootTime;
	}

	public void setLastShootTime(long lastShootTime) {
		this.lastShootTime = lastShootTime;
	}

	protected void setup() {
		if (!setServiceAgents()) {
			doDelete();
			return;
		}
		
		setLanguages();
		
		try {
			Object[] obj = getArguments();
			chart = (Chart) obj[0];
		} catch (Exception e) {
			logger.error("{}: Błąd podczas setup()", getName(), e);
			doDelete();
			return;
		}
		
		position = PositionHelper.getRandomPosition(this);
		
		r = AgentColor.HUNTER_COLOR[0];
		g = AgentColor.HUNTER_COLOR[1];
		b = AgentColor.HUNTER_COLOR[2];
	
		addHuntingBehaviour();
		addBehaviour(new ShootBehaviour(this));
		addBehaviour(new ShootSynchronizationServerBehaviour(this));
		addBehaviour(new DFRegisterBehaviour(this, HUNTER_SERVICE_TYPE));
	}
	
	public String getServiceType() {
		return HunterAgent.HUNTER_SERVICE_TYPE;
	}

	public double getScareFactor() {
		return scareFactor;
	}

	public void setScareFactor(double scareFactor) {
		this.scareFactor = scareFactor;
	}

	public void setPairedLeader(AID pairedLeader) {
		this.pairedLeader = pairedLeader;
	}
	
	public AID getPairedLeader() {
		return pairedLeader;
	}

	@Override
	public void activate() {
		setColor(AgentColor.ACTIVE_COLOR[0],
				AgentColor.ACTIVE_COLOR[1],
				AgentColor.ACTIVE_COLOR[2]);
	}

	@Override
	public void scare() {
		addBehaviour(new ScaringBehaviour(this));
	}
}
